package com.weipai.springboot.module.service.impl;

import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Role;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.mapper.RoleMapper;
import com.weipai.springboot.module.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author songmengqiang
 * @create 2018-08-21 18:21
 **/
@Service
public class RoleServiceImpl implements RoleService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Response list(String draw) {
        Response response = new Response();
        List<Map<String, Object>> list = roleMapper.selectAllByDel();
        if (list.size() > 0) {
            PageResult<Map<String, Object>> pageList = new PageResult<>(list, 1, 10000, list.size(), draw);
            response.put("pageList", pageList);
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response save(Role role) {
        Response response = new Response();
        int r = roleMapper.insertSelective(role);
        if (r == 1) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response getRole(Long id) {
        Response response = new Response();
        Role role = roleMapper.selectByPrimaryKey(id);
        if (role != null) {
            response.setCode(RetCode.SUCCESS);
            response.put("role", role);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response update(Role role) {
        Response response = new Response();
        int r = roleMapper.updateByPrimaryKeySelective(role);
        if (r == 1) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response deleteById(Long id) {
        Response response = new Response();
        int r = roleMapper.deleteByPrimaryKey(id);
        if (r == 1) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response batchDeleteById(Long[] id) {
        Response response = new Response();
        try {
            for (int i = 0; i < id.length; i++) {
                roleMapper.deleteByPrimaryKey(id[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Role getRoleObject(Long id) {
        return  roleMapper.selectByPrimaryKey(id);
    }
}
